//
//  LocationModel.m
//  lumihui
//
//  Created by oceans on 2018/10/27.
//  Copyright © 2018 ios. All rights reserved.
//

#import "LocationModel.h"
#import <MapKit/MapKit.h>
#import "AppDelegate.h"

@interface LocationModel () <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@end

@implementation LocationModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
    if ([CLLocationManager locationServicesEnabled] == NO) {
        self.locationStatus = LocationStatusNoServices;
        return;
    }
    
    self.locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined) {
        self.locationStatus = LocationStatusNotRequest;
    }else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
        self.locationStatus = LocationStatusNotAllow;
    }else {
        self.locationStatus = LocationStatusAllow;
    }
}

- (void)startLocation {
    
    if ([CLLocationManager locationServicesEnabled] == NO) {
        self.locationStatus = LocationStatusNoServices;
        return;
    }
    
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined) {
        self.locationStatus = LocationStatusNotRequest;
        [self.locationManager requestWhenInUseAuthorization];
    }else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
        self.locationStatus = LocationStatusNotAllow;
        UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //打开定位设置
            NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:settingsURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @YES} completionHandler:nil];
            }else {
                [[UIApplication sharedApplication] openURL:settingsURL];
            }
        }];
        UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:cancel];
        [alertVC addAction:ok];
        AppDelegate *appdel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        if (appdel.root.presentedViewController) {
            [appdel.root.presentedViewController presentViewController:alertVC animated:YES completion:nil];
        }else {
            [appdel.root presentViewController:alertVC animated:YES completion:nil];
        }
    }else {
        self.locationStatus = LocationStatusLocating;
        [self.locationManager startUpdatingLocation];
    }
}

- (void)setDelegate:(id<LocationModelDelegate>)delegate {
    _delegate = nil;
    _delegate = delegate;
    delegate = nil;
    [self.delegate locationModel:self statusChanged:_locationStatus];
}

- (void)setLocationStatus:(LocationStatus)locationStatus {
    _locationStatus = locationStatus;
    [self.delegate locationModel:self statusChanged:_locationStatus];
}

#pragma mark - cllocation delegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusNotDetermined) {
        self.locationStatus = LocationStatusNotAllow;
    }else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
        self.locationStatus = LocationStatusNotAllow;
    }else {
        self.locationStatus = LocationStatusLocating;
        [self.locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [self.locationManager stopUpdatingLocation];
    if (_locationStatus == LocationStatusFinish) {
        return;
    }
    
    self.locationStatus = LocationStatusFinish;
    [self.delegate locationModel:self locationFinished:locations];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [self.locationManager stopUpdatingLocation];
    self.locationStatus = LocationStatusFailed;
    [self.delegate locationModel:self locationFailed:error];
}

@end
